<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;

class AttendanceRule extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'code',
        'type',
        'description',
        'work_start_time',
        'work_end_time',
        'break_start_time',
        'break_end_time',
        'flexible_minutes',
        'allow_overtime',
        'late_tolerance_minutes',
        'early_leave_tolerance_minutes',
        'allow_makeup',
        'monthly_makeup_limit',
        'makeup_approval_required',
        'location_restriction',
        'location_data',
        'applicable_departments',
        'applicable_positions',
        'applicable_users',
        'is_shift_work',
        'shift_type',
        'shift_schedule',
        'status',
        'sort_order',
    ];

    protected $casts = [
        'work_start_time' => 'datetime:H:i',
        'work_end_time' => 'datetime:H:i',
        'break_start_time' => 'datetime:H:i',
        'break_end_time' => 'datetime:H:i',
        'allow_overtime' => 'boolean',
        'allow_makeup' => 'boolean',
        'is_shift_work' => 'boolean',
        'location_data' => 'array',
        'applicable_departments' => 'array',
        'applicable_positions' => 'array',
        'applicable_users' => 'array',
        'shift_schedule' => 'array',
    ];

    // 常量定义
    const TYPE_FIXED = 'fixed';
    const TYPE_FLEXIBLE = 'flexible';
    const TYPE_SHIFT = 'shift';
    const TYPE_NO_CHECK = 'no_check';

    const LOCATION_NONE = 'none';
    const LOCATION_FIXED = 'fixed';
    const LOCATION_AREA = 'area';
    const LOCATION_GPS = 'gps';

    const SHIFT_2 = '2_shift';
    const SHIFT_3 = '3_shift';
    const SHIFT_4 = '4_shift';

    /**
     * 获取打卡记录关系
     */
    public function clockInRecords(): HasMany
    {
        return $this->hasMany(ClockInRecord::class);
    }

    /**
     * 获取考勤统计关系
     */
    public function attendanceStatistics(): HasMany
    {
        return $this->hasMany(AttendanceStatistic::class);
    }

    /**
     * 作用域：活跃规则
     */
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    /**
     * 作用域：按类型筛选
     */
    public function scopeByType($query, $type)
    {
        return $query->where('type', $type);
    }

    /**
     * 检查用户是否适用此规则
     */
    public function isApplicableToUser($userId, $departmentId = null, $positionId = null)
    {
        // 检查具体用户
        if ($this->applicable_users && in_array($userId, $this->applicable_users)) {
            return true;
        }

        // 检查岗位
        if ($positionId && $this->applicable_positions && in_array($positionId, $this->applicable_positions)) {
            return true;
        }

        // 检查部门
        if ($departmentId && $this->applicable_departments && in_array($departmentId, $this->applicable_departments)) {
            return true;
        }

        return false;
    }

    /**
     * 获取规则类型的显示名称
     */
    public function getTypeNameAttribute()
    {
        return match($this->type) {
            self::TYPE_FIXED => '固定时间',
            self::TYPE_FLEXIBLE => '弹性时间',
            self::TYPE_SHIFT => '倒班制',
            self::TYPE_NO_CHECK => '不考勤',
            default => '未知'
        };
    }

    /**
     * 获取位置限制类型的显示名称
     */
    public function getLocationRestrictionNameAttribute()
    {
        return match($this->location_restriction) {
            self::LOCATION_NONE => '无限制',
            self::LOCATION_FIXED => '固定位置',
            self::LOCATION_AREA => '区域限制',
            self::LOCATION_GPS => 'GPS定位',
            default => '未知'
        };
    }
}
